package cn.com.hhrcw.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * @author lixingxing
 */
@Slf4j
public class ThirdLoginUtil {
    private static final ResourceBundle RB = ResourceBundle.getBundle("conf.ThirdLogin");

    /**
     * PC端微信登录
     */
    public JSONObject pcWxCallback(String code) {
        //https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
        Map<String, String> params = new HashMap<>(5);
        //不知道url填什么可以看文档：https://open.weibo.com/wiki/Oauth2/access_token
        //通过code获取token + openid
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token";
        //申请应用时分配的AppKey
        params.put("appid", RB.getString("ThirdLogin.PCWXappid"));
        //申请应用时分配的AppSecret
        params.put("secret", RB.getString("ThirdLogin.PCWXsecret"));
        //请求的类型，填写authorization_code
        params.put("grant_type", RB.getString("ThirdLogin.PCWXgrant_type"));
        //调用authorize获得的code值
        params.put("code", code);
        try {
            String result = HttpUtil.getWxTokenOid(url, params);

            JSONObject jsonObject = (JSONObject) JSONObject.parse(result);
            //通过token + openid获取用户信息
            url = "https://api.weixin.qq.com/sns/userinfo";
            String getUserInfo = HttpUtil.wxGetUserInfo(url, jsonObject.get("access_token"), jsonObject.get("openid"));
            JSONObject parse = (JSONObject) JSONObject.parse(getUserInfo);
            return parse;

        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * PC端qq登录
     *
     * @param code
     * @return
     */
    public JSONObject qqCallback(String code) {
        //https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
        Map<String, String> params = new HashMap<>(5);
        //不知道url填什么可以看文档：https://open.weibo.com/wiki/Oauth2/access_token
        //通过code获取token
        String url = "https://graph.qq.com/oauth2.0/token";
        //申请应用时分配的AppKey
        params.put("client_id", RB.getString("ThirdLogin.PCQQclient_id"));
        //申请应用时分配的AppSecret
        params.put("client_secret", RB.getString("ThirdLogin.PCQQclient_secret"));
        //请求的类型，填写authorization_code
        params.put("grant_type", RB.getString("ThirdLogin.PCQQgrant_type"));
        //调用authorize获得的code值
        params.put("code", code);
        params.put("redirect_uri", RB.getString("ThirdLogin.PCQQredirect_uri"));

        try {
            String result = HttpUtil.qqGetToken(url, params);
            String[] split = result.split("&");

            String accessToken = split[0];
            String[] split1 = accessToken.split("=");

            if (split1.length == 1) {
                log.info("code码过期！");
                return null;
            } else {
                String token = split1[1];
                //通过token获取openid
                String url2 = "https://graph.qq.com/oauth2.0/me";
                String getopenid = HttpUtil.qqGetOpenid(url2, token);
                getopenid = getopenid.replaceAll("callback\\( ", "");
                getopenid = getopenid.substring(0, getopenid.length() - 3);

                JSONObject parse = JSON.parseObject(getopenid);
                //通过token + openid + client_id(appid)获取用户信息
                String url3 = "https://graph.qq.com/user/get_user_info";
                String getUserinfo = HttpUtil.qqGetUserInfo(url3, token, parse.get("openid"), parse.get("client_id"));
                JSONObject userinfo = (JSONObject) JSONObject.parse(getUserinfo);
                userinfo.put("openid", parse.get("openid"));
                userinfo.put("unionid", parse.get("unionid"));
                return userinfo;
            }

        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     *
     * APP端qq登录
     *
     * */

    public JSONObject appQqCallback(String token) {

        try {
            //通过token获取openid
            String url2 = "https://graph.qq.com/oauth2.0/me";
            String getopenid = HttpUtil.qqGetOpenid(url2, token);
            getopenid = getopenid.replaceAll("callback\\( ", "");
            getopenid = getopenid.substring(0, getopenid.length() - 3);

            JSONObject parse = JSON.parseObject(getopenid);
            //通过token + openid + client_id(appid)获取用户信息
            String url3 = "https://graph.qq.com/user/get_user_info";
            String getUserinfo = HttpUtil.qqGetUserInfo(url3, token, parse.get("openid"), parse.get("client_id"));

            JSONObject userinfo = (JSONObject) JSONObject.parse(getUserinfo);
            userinfo.put("openid", parse.get("openid"));
            userinfo.put("unionid", parse.get("unionid"));

            return userinfo;

        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     *
     * APP端微信登录
     *
     * */
    public JSONObject appWxCallback(String code) {
        //https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
        Map<String, String> params = new HashMap<>(5);
        //不知道url填什么可以看文档：https://open.weibo.com/wiki/Oauth2/access_token
        //通过code获取token + openid
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token";
        //申请应用时分配的AppKey
        params.put("appid", RB.getString("ThirdLogin.APPWXclient_id"));
        //申请应用时分配的AppSecret
        params.put("secret", RB.getString("ThirdLogin.APPWXient_secret"));
        //请求的类型，填写authorization_code
        params.put("grant_type", RB.getString("ThirdLogin.APPWXgrant_type"));
        //调用authorize获得的code值
        params.put("code", code);
        try {
            String result = HttpUtil.getWxTokenOid(url, params);

            JSONObject jsonObject = (JSONObject) JSONObject.parse(result);
            //通过token + openid获取用户信息
            url = "https://api.weixin.qq.com/sns/userinfo";
            String getUserInfo = HttpUtil.wxGetUserInfo(url, jsonObject.get("access_token"), jsonObject.get("openid"));
            JSONObject parse = (JSONObject) JSONObject.parse(getUserInfo);
            return parse;

        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     *
     * APP端微博登录
     *
     * */
    public JSONObject appWbCallback(String code) {
        System.out.println("得到的code为：" + code);
        Map<String, String> params = new HashMap<>(5);
        //不知道url填什么可以看文档：https://open.weibo.com/wiki/Oauth2/access_token
        //通过code获取access_token + uid接口
        String url = "https://api.weibo.com/oauth2/access_token";
        //申请应用时分配的AppKey
        params.put("client_id", RB.getString("ThirdLogin.APPWBclient_id"));
        //申请应用时分配的AppSecret
        params.put("client_secret", RB.getString("ThirdLogin.APPWBclient_secret"));
        //请求的类型，填写authorization_code
        params.put("grant_type", RB.getString("ThirdLogin.APPWBgrant_type"));
        //调用authorize获得的code值
        params.put("code", code);
        //回调地址，需需与注册应用里的回调地址一致。
        params.put("redirect_uri", RB.getString("ThirdLogin.APPWBredirect_uri"));
        try {
            String result = HttpUtil.getWbTokenUid(url, params);

            JSONObject jsonObject = (JSONObject) JSONObject.parse(result);
            System.out.println(jsonObject.get("access_token"));
            //通过token + uid 获取用户信息
            url = "https://api.weibo.com/2/users/show.json";
            String getUserInfo = HttpUtil.wbget(url, jsonObject.get("access_token"), jsonObject.get("uid"));
            JSONObject parse = (JSONObject) JSONObject.parse(getUserInfo);
            return parse;
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

}
